// *******************************************************************************************************
// # INDEX
//   / トップ
// *******************************************************************************************************
this.rjapmn_md = this.rjapmn_md || {};

(function(){
	rjapmn_md.data = {};

	rjapmn_md.data.posArray = [null, "init"];

	rjapmn_md.data.INTERVAL_FADEIN = 800;
	rjapmn_md.data.INTERVAL_DOOR_TWEEN = 1200;
	rjapmn_md.data.INTERVAL_DOOR_WAIT = 500;

	rjapmn_md.data.loadImgArray = [
		"/static/top/modal/images/bg_modal.png",
		"/static/top/modal/images/img_chara.png",
		"/static/top/modal/images/face_greeting.gif",
		"/static/top/modal/images/img_desk_chair.png",
		"/static/top/modal/images/img_tree.png",
		"/static/top/modal/images/img_pc.png",
		"/static/top/modal/images/img_chair_l.png",
		"/static/top/modal/images/img_door_l.png",
		"/static/top/modal/images/img_door_r.png",
		"/static/top/modal/images/btn_back_off.gif",
		"/static/top/modal/images/btn_search_off.gif",
		"/static/top/modal/images/btn_s1_1_off.gif",
		"/static/top/modal/images/btn_s1_2_off.gif",
		"/static/top/modal/images/btn_s1_3_off.gif",
		"/static/top/modal/images/btn_s1_4_off.gif",
		"/static/top/modal/images/btn_s1_5_off.gif",
		"/static/top/modal/images/btn_s1_6_off.gif",
		"/static/top/modal/images/btn_s1_7_off.gif"
	];

	rjapmn_md.data.is_cookie=false;
	rjapmn_md.data.firstVisitCookieArray = [];
	
	rjapmn_md.data.COOKYE = 'apmnFirstVisit';

	rjapmn_md.data.btnActiveObj = {"scene1":undefined, "scene2":undefined, "scene3":undefined, "scene4":undefined};
	rjapmn_md.data.dataObj = {"scene1":undefined, "scene2":undefined, "scene3":undefined};

	rjapmn_md.data.numbersArray = {
		"1人暮らし":"ワンルーム/1K/1DK/1LDK/2K/2DK",
		"2人暮らし":"1LDK/2K/2DK/2LDK/3K/3DK/3LDK",
		"ファミリー":"2LDK/3K/3DK/3LDK/4K/4DK/4LDK/5K以上"
	}

	//----------------------------------------------------------------------------
	// INIT - Event
	//----------------------------------------------------------------------------
	rjapmn_md.init = function(){
		rjapmn_md.onLoadMainEvent();
		rjapmn_md.initInputText();

		initCookieVisit();
	}

	//---------------------------------------------------------------------------
	//Event - onContentEvent
	//---------------------------------------------------------------------------
	rjapmn_md.setContent = function(obj){
		rjapmn_md.data.posArray.shift();
		rjapmn_md.data.posArray.push(obj);

		rjapmn_md.onContentEvent(rjapmn_md.data.posArray[1]);
	},

	rjapmn_md.getContent = function(str){
		var _str;
		if(str == "pre")_str = rjapmn_md.data.posArray[0];
		else if(str == "now")_str = rjapmn_md.data.posArray[1];

		return _str;
	}

	//----------------------------------------------------------------------------
	// INIT - BtnMap
	//----------------------------------------------------------------------------
	rjapmn_md.initBtnScene = function(){

		//次へボタン
		for(var i=1; i<5; i++){
			initNextAndSearchBtn(i);

			if(i>1){
				//戻るボタン
				initBackBtn(i);
			}

			//各リストボタン(scene2 and scene3)
			if(i>1 && i<4){
				initListBtn(i);
			}
		}

		//----------------------------------
		//DISPLAY NONE;
		//----------------------------------
		for(var k=2;k<5;k++){
			$('#scene'+k).css({display:'none'});
		}

		function initNextAndSearchBtn(i){
			var num = i+1;
			$('#scenearea #scene'+i+' .btn-search').on('click', function(e){
				var now = rjapmn_md.getContent('now');

				if(now == 'scene1'){

					_value = $('#scene1 input').attr('value');

					if($('#input-station').val() != _value){
						setFirstVisitLoacalData('scene1', $('#input-station').val());
						rjapmn_md.setContent("scene2");
					}

				}else if(now == 'scene2'){

					if(rjapmn_md.data.btnActiveObj['scene2'] != undefined){
						rjapmn_md.setContent("scene3");
					}

				}else if(now == 'scene3'){

					if(rjapmn_md.data.btnActiveObj['scene3'] != undefined){
						rjapmn_md.setContent("scene4");
					}

				}else{
					rjapmn_md.setContent("scene"+num);
				}	
			});		
		}

		function initBackBtn(i){
			var num = i-1;
			$('#scenearea #scene'+i+' .btn-back').on('click', function(e){
				rjapmn_md.setContent("scene"+num);
			});		
		}

		function initListBtn(i){
			$('#scenearea #scene'+i+' li a').on('click', function(e){
				e.preventDefault();
				var num = $('#scenearea #scene'+i+' li a').index(this);

				if(rjapmn_md.data.btnActiveObj['scene'+i] != num){
					if(rjapmn_md.data.btnActiveObj['scene'+i] != undefined){
						var pre_li = $('#scenearea #scene'+i+' li a:eq('+rjapmn_md.data.btnActiveObj['scene'+i]+')');
						// pre_li.addClass('mover');

						rjapmn_md.onImgClickReplace.off($('img',pre_li));

						pre_li.parent().addClass('mover');
					}
					rjapmn_md.data.btnActiveObj['scene'+i] = num;
					rjapmn_md.onImgClickReplace.on($('img',this));

					//クリックされたボタンの'alt'テキストをデータとして、rjapmn_md.data.dataObjに登録します。
					//altテキストでなく、他に任意のデータを登録したい場合は、下記、代入する値を変更してください。
					//rjapmn_md.data.dataObj['scene'+i]=$('#scene'+i+' li a:eq('+rjapmn_md.data.btnActiveObj['scene'+i]+') img').attr('alt');
					var _scene = 'scene'+i;
					setFirstVisitLoacalData(_scene, $('#scene'+i+' li a:eq('+rjapmn_md.data.btnActiveObj['scene'+i]+') img').attr('alt'));

					$(this).parent().removeClass('mover');
				}
			});	
		}

		function setFirstVisitLoacalData(_scene, obj){
			rjapmn_md.data.dataObj[_scene] = obj;
		}
	}

	//----------------------------------------------------------------------------
	// BTN - Click Replace
	//----------------------------------------------------------------------------
	rjapmn_md.onImgClickReplace = {
		on:function(obj){obj.attr('src', obj.attr('src').replace('_off', '_stay'));},
		off:function(obj){obj.attr('src', obj.attr('src').replace('_stay', '_off'));}
	}

	//----------------------------------------------------------------------------
	// BTN - Check Active
	//----------------------------------------------------------------------------
	rjapmn_md.checkBtnActive = function(){
		var now = rjapmn_md.getContent('now');
		var num = rjapmn_md.data.btnActiveObj[now];

		if(num != undefined){
			var _scene = $('img', '#scenearea #'+now+' li:eq('+num+')');
			_scene.attr('src', _scene.attr('src').replace('_off', '_stay'));
		}
	}

	//----------------------------------------------------------------------------
	// EVENT - Comment Show or Hide
	//----------------------------------------------------------------------------
	rjapmn_md.onCommentShowHide = function(_scene, b){
		if(b){
			$('#comarea .com_'+_scene).css({display:'block'})
				.delay(500)
				.animate({opacity:1}, 700, "easeOutQuart");
		}else{
			$('#comarea .com_'+_scene)
				.animate({opacity:0}, 300, "easeOutQuart")
				.css({display:'none'});
		}
	}

	rjapmn_md.onFaceAnimEvent = function(_scene){
		$('#face_scene').remove();
		$('#facearea').prepend(rjapmn_md.getHtmlFaceareaSource(_scene));
	}

	rjapmn_md.getHtmlFaceareaSource = function(_scene){
		var _source="";
		_source +='<div id="face_scene">';
		_source += '<img src="/static/top/modal/images/face_'+_scene+'.gif" width="130" height="92" alt="" />';
		_source += '</div>';

		return _source;
	}

	//----------------------------------------------------------------------------
	// EVENT - MAIN
	//----------------------------------------------------------------------------
	rjapmn_md.onContentEvent = function(str){

		var pre = rjapmn_md.getContent('pre');
		var now = str;

		switch(now){
			case "opening":
			
				//loader削除
				//rjapmn.onLoadingEvent.complete();

				//各ボタンINITしたら、display:noneにしないと
				rjapmn_md.initBtnScene();

				$("#firstwrapper #contents-md").css({display:"block"})
				//top.js
				.animate({
					opacity:1 
				}, rjapmn_md.data.INTERVAL_FADEIN, "easeOutQuart", function() {
					rjapmn_md.setContent("doorOpen");
				});
			break;

			case "doorOpen":
				$("#door_l").delay(rjapmn_md.data.INTERVAL_DOOR_WAIT).animate({marginLeft:"-470px" }, rjapmn_md.data.INTERVAL_DOOR_TWEEN, "easeOutExpo", function() {});
				$("#door_r").delay(rjapmn_md.data.INTERVAL_DOOR_WAIT).animate({marginLeft:"930px" }, rjapmn_md.data.INTERVAL_DOOR_TWEEN, "easeOutExpo", function() {
					rjapmn_md.setContent("greeting");
				});
			break;

			case "greeting":

				$('#greeting').css({display:'block', opacity:1});

				$('#greeting .fukidashi img')
				.css({width:'10px', height:'10px', opacity:1})
				.animate({width:'252px', height:'150px'}, 400, "easeOutQuint");

				$('#greeting .com_greeting')
				.delay(500)
				.animate({opacity:'1'}, 600, "easeOutQuint");

				$('#greeting')
				.delay(2000)
				.animate({opacity:0}, 300, "easeOutQuart", function() {
					$('#greeting').css({display:'none'})
					rjapmn_md.setContent("scene1");
				});

			break;

			case "scene1":

				if(pre == "greeting"){
					$('#chara').delay(50).animate({marginLeft:'190px'}, 700, "easeOutCubic");
					$('#facearea').delay(50).animate({left:'239px'}, 700, "easeOutCubic");
					$('#tree').delay(100).animate({marginLeft:'-78px'}, 800, "easeOutCubic");
					$('#pc').animate({marginLeft:'20px'}, 600, "easeOutCubic");
					$('#chair_l').animate({marginLeft:'-120px'}, 600, "easeOutCubic");

					//質問スライドイン
					$('#md-quest').delay(100).animate({left:'436px'}, 700, "easeOutQuart");


					rjapmn_md.onCommentShowHide('scene1', true);
					//rjapmn_md.onFaceAnimEvent('greeting');


				}else if(pre == "scene2"){
					$('#scene2')
					.animate({marginLeft:'-450px'}, 400, "easeOutQuart", function() {
						$('#scene2').css({marginLeft:0, display:'none'});
					});

					$('#scene1').css({display:'block',marginLeft:'450px'})
					.animate({marginLeft:'0px'}, 400, "easeOutQuart", function() {
						rjapmn_md.checkBtnActive();
					});

					rjapmn_md.onCommentShowHide(pre, false);
					rjapmn_md.onCommentShowHide(now, true);
				}
			break;

			case "scene2":

				if(pre == "scene1"){
					$('#scene1')
					.animate({marginLeft:'-450px'}, 400, "easeOutQuart", function() {
						$('#scene1').css({marginLeft:'0px', display:'none'});
					});

					$('#scene2').css({display:'block',marginLeft:'450px'})
					.animate({marginLeft:'0px'}, 400, "easeOutQuart", function() {
						rjapmn_md.checkBtnActive();
					});

				}else if(pre =="scene3"){

					$('#scene3')
					.animate({marginLeft:'-450px'}, 400, "easeOutQuart", function() {
						$('#scene3').css({marginLeft:'0px', display:'none'});
					});

					$('#scene2').css({display:'block',marginLeft:'450px'})
					.animate({marginLeft:0}, 400, "easeOutQuart", function() {
						rjapmn_md.checkBtnActive();
					});

				}

				rjapmn_md.onCommentShowHide(pre, false);
				rjapmn_md.onCommentShowHide(now, true);
			break;

			case "scene3":

				if(pre == "scene2"){
					$('#scene2')
					.animate({marginLeft:'-450px'}, 400, "easeOutQuart", function() {
						$('#scene2').css({marginLeft:0, display:'none'});
					});

					$('#scene3').css({display:'block',marginLeft:'450px'})
					.animate({marginLeft:'0px'}, 400, "easeOutQuart", function() {
						rjapmn_md.checkBtnActive();
					});

				}else if(pre =="scene4"){
					$('#scene4').animate({marginLeft:'-450px'}, 400, "easeOutQuart", function() {
						$('#scene4').css({marginLeft:'0px', display:'none'});
					});

					$('#scene3').css({display:'block',marginLeft:'450px'})
					.animate({marginLeft:'0px'}, 400, "easeOutQuart", function() {
						rjapmn_md.checkBtnActive();
					});
				}

				rjapmn_md.onCommentShowHide(pre, false);
				rjapmn_md.onCommentShowHide(now, true);
			break;

			case "scene4":

				if(pre == "scene3"){
					$('#scene3')
					.animate({marginLeft:'-450px'}, 400, "easeOutQuart", function() {
						$('#scene3').css({marginLeft:0, display:'none'});
					});

					$('#scene4').css({display:'block',marginLeft:'450px'});

					rjapmn_md.onCreateScene4Html();

					$('#scene4').animate({marginLeft:'0px'}, 400, "easeOutQuart", function() {
						if(rjapmn_md.data.btnActiveObj["scene4"] == undefined){
							rjapmn_md.data.btnActiveObj["scene4"] = true;
						}
					});
				}

				rjapmn_md.onCommentShowHide(pre, false);
				rjapmn_md.onCommentShowHide(now, true);
			break;

			case "scene5":

				//alert("駅名：今はなし /"+"家賃："+rjapmn_md.data.dataObj['scene2']+"/居住人数："+rjapmn_md.data.dataObj['scene3']);

				//apmnDeleteCookie(rjapmn_md.data.COOKYE);
				//apmnSetCookieAll([rjapmn_md.data.dataObj['scene1'],rjapmn_md.data.dataObj['scene2'],rjapmn_md.data.dataObj['scene3']]);
				//apmnCreateCookieDataArray("-", apmnGetCookie(rjapmn_md.data.COOKYE));

				apmnSetCookie(1);
				apmnSetCookie(2);
				apmnSetCookie(3);

				alert("Cookie/駅名="+getFirstVisitCookie1());
				alert("Cookie/家賃="+getFirstVisitCookie2());
				alert("Cookie/居住人数="+getFirstVisitCookie3());

				rjapmn_md.onCommentShowHide(pre, false);
				
			break;
		}
	}

	rjapmn_md.onCreateScene4Html = function(){
		$('#wrapper-confirm').remove();
		$('#scene4-confirm').prepend(rjapmn_md.getHtmlScene4Source());
	}

	rjapmn_md.getHtmlScene4Source = function(){
		var _source="";
		_source +='<div id="wrapper-confirm">';
		_source += '<ul>';
		_source += '<li class="station">'+rjapmn_md.data.dataObj['scene1']+'</li>';
		_source += '<li class="rent">'+rjapmn_md.data.dataObj['scene2']+'</li>';
		_source += '<li class="numbers">'+rjapmn_md.data.dataObj['scene3']+'<span class="araeofroom">'+rjapmn_md.data.numbersArray[rjapmn_md.data.dataObj['scene3']]+'</span></li>';
		_source += '</ul>';
		_source += '</div>';

		return _source;
	}

	//----------------------------------------------------------------------------
	// LOAD
	//----------------------------------------------------------------------------
	rjapmn_md.onLoadMainEvent = function(){
		var len = rjapmn_md.data.loadImgArray.length;
		rjapmn_md.data.loadcount = len;

		for(var i=0; i<len; i++){
			rjapmn_md.loaderUtil.init(i, rjapmn_md.data.loadImgArray[i], rjapmn_md, "onLoadMainComplete");
		}
	}

	rjapmn_md.onLoadMainComplete = function(){
	    	rjapmn_md.data.loadcount--;
	    	if(rjapmn_md.data.loadcount == 0){
	    		rjapmn_md.setContent("opening");
	    	}
	}

	rjapmn_md.loaderUtil={
		_scope:[],
		_callback:"",
		_imgArray:[],
		
		init:function(num,url,scope,func){
			this._scope[0]=scope;
			this._callback=func;
			this._imgArray[num]=new Image();
			this._imgArray[num].onload=rjapmn_md.loaderUtil.onComplete;
			this._imgArray[num].src=url;
		},

		onComplete:function(){
			(function(){rjapmn_md.loaderUtil._scope[0][rjapmn_md.loaderUtil._callback]()})();
		}
	}

	//*****************************************************************************************
	//----------------------------------------------------------------------------
	// INIT - Cookie Visit
	//----------------------------------------------------------------------------
	function initCookieVisit(){
		if (navigator.cookieEnabled) {
			rjapmn_md.data.is_cookie = true;
		}else {
			//You can't cookie
		}
	}

	/**
	* Cookie - get data
	*/
	function getFirstVisitCookieStation(){return rjapmn_md.data.firstVisitCookieArray[0];}
	function getFirstVisitCookieRent(){return rjapmn_md.data.firstVisitCookieArray[1];}
	function getFirstVisitCookieNumbers(){return rjapmn_md.data.firstVisitCookieArray[2];}

	function getFirstVisitCookie(){return apmnGetCookie(rjapmn_md.data.COOKYE);}


	function getFirstVisitCookie1(){return apmnGetCookie('rjapmnFirstVisit1');}
	function getFirstVisitCookie2(){return apmnGetCookie('rjapmnFirstVisit2');}
	function getFirstVisitCookie3(){return apmnGetCookie('rjapmnFirstVisit3');}


	/**
	* Cookie - get data
	*/
	function apmnGetCookie(str){
		var result = null;
		var cookieName=str+'=';
		var allcookies = document.cookie;

		var position = allcookies.indexOf(cookieName);

		if(position != -1){
			var startIndex = position + cookieName.length;
			var endIndex = allcookies.indexOf(';',startIndex);

			if(endIndex == -1){
				endIndex = allcookies.length;
			}
			result = decodeURIComponent(allcookies.substring(startIndex, endIndex));
		}
		return result;
	}

	function apmnSetCookie(num){
		if(rjapmn_md.data.is_cookie){
			var _cookie = "";
			_cookie += rjapmn_md.data.dataObj["scene"+num];
			document.cookie = 'rjapmnFirstVisit'+num+'=' + _cookie + apmnCreateDateControl(1);
		}
	}

	/**
	* Cookie ALL - create
	*/
	function apmnSetCookieAll(ary){
		if(rjapmn_md.data.is_cookie){
			var _cookie = "";
			var _legth = ary.length;
			var _lengthMin = _legth-1;

			for(var i=0; i<_lengthMin; i++){
				_cookie += ary[i];
				_cookie += "-"
			}
			_cookie += ary[_lengthMin];
			document.cookie = rjapmn_md.data.COOKYE+'=' + _cookie + apmnCreateDateControl(1);
		}
	}

	/**
	* Cookie - create array data
	*/
	function apmnCreateCookieDataArray(str, ary){
		if(rjapmn_md.data.is_cookie){
			var resArray = ary.split(str);
			var ret = "";
			for( var i=0 ; i<resArray.length ; i++ ) {
				rjapmn_md.data.firstVisitCookieArray[i] = resArray[i];
			}
		}
	}

	/**
	* Cookie - delete
	*/
	function apmnDeleteCookie(obj) {
		cName = obj+'=';
		document.cookie = cName  + apmnCreateDateControl(-1);
	}

	/**
	* Create date
	*/
	function apmnCreateDateControl(dir){
		var str="";
		var expires;
		var nowtime = new Date().getTime();
		
		expires = new Date(nowtime + (60 * 60 * 24 * 1000 * 3 * dir));
		//expires = new Date(nowtime + (60 * 60 * 24 * 1000 * dir));
		
		expires = expires.toGMTString();
		str += "; expires=" + expires;
		return str;
	}


	/*******************************************************
	 *　initInputText
	 *******************************************************/
	rjapmn_md.initInputText = function(){
		var inputTextBox = $('#questarea input:text');

		inputTextBox.each(function(i){
			$(this).focus(function(e){
				$(this).addClass('input-focus');
				if(this.value == this.defaultValue){
					$(this).val('');
				};
			}).blur(function(e){
				if(this.value == ''){
					$(this).val(this.defaultValue).removeClass('input-focus');
				};
			});
			if (this.value != this.defaultValue) {
				$(this).addClass('input-focus');
			};
		});
	};
})();

//----------------------------------------------------------------------------
// INIT
//----------------------------------------------------------------------------
$(document).ready(
	function(){
		rjapmn_md.init();
	}
);
